<?php

namespace App\Admin;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    //定义关联的数据表
    protected $table = 'role';
    //禁用时间字段
    public $timestamps = false;

    protected $fillable    =  ['role_name','auth_ids','auth_ac','isdel'];

    //assignAuth方法处理分配权限
    /**
     * @Author   Y
     * @DateTime 2017-11-21
     * @param    [array]     $data [其中包含了记录id和auth_id数组权限集合]
     * @return   [int]           [修改操作结果，受到影响的行数]
     */
    public function assignAuth($data){
    	//auth_ids字段
    	//$post['auth_ids'] = implode($data['auth_id'],',');【可以调换顺序】
    	$post['auth_ids'] = implode(',',$data['auth_id']);
    	//查询auth表中的对应的记录取出A和C做连接操作
    	$auth = Auth::where('pid','>','0') -> whereIn('id',$data['auth_id']) -> get();
    	$ac = '';
    	foreach ($auth as $key => $value) {
    		//拼凑控制器和方法
    		$ac .= $value -> controller . '@' . $value -> action . ',';
    	}
    	//去除末尾的逗号
    	$post['auth_ac'] = rtrim($ac,',');
    	//修改保存操作
    	return Role::where('id','=',$data['id']) -> update($post);
    }

    public function addRole($data){
        $post['role_name'] = $data['role_name'];
        $post['isdel'] = '2';
        //auth_ids字段
        //$post['auth_ids'] = implode($data['auth_id'],',');【可以调换顺序】
        $post['auth_ids'] = implode(',',$data['auth_id']);
        //查询auth表中的对应的记录取出A和C做连接操作
        $auth = Auth::where('pid','>','0') -> whereIn('id',$data['auth_id']) -> get();
        $ac = '';
        foreach ($auth as $key => $value) {
            //拼凑控制器和方法
            $ac .= $value -> controller . '@' . $value -> action . ',';
        }
        //去除末尾的逗号
        $post['auth_ac'] = rtrim($ac,',');
        //修改保存操作
        return Role::create($post)?'1':'0';
    }
}
